package com.haogu.controller;

import com.haogu.entity.*;
import com.haogu.service.FunctionService;
import com.haogu.util.ResultData;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system")
public class FunctionManagerController {
    @Resource
    private ResultData resultData;
    @Resource
    private FunctionService functionService;
    @RequestMapping("/toListFunction")
    public String toListUser(){
        return "system/list_function2";
    }

    @RequestMapping("/loadAllFunction")
    @ResponseBody
    public Map loadAllFunction(Integer page,Integer rows){
        page = page == null ? 1 : page;
        rows = rows == null ? 10 : rows;

        List<VFunction> vFunctions = functionService.selectAllFunction(page, rows);
        Integer integer = functionService.selectCount();
        Map map = new HashMap();
        map.put("totalRows",integer);
        map.put("result",vFunctions);
        Map map1 = new HashMap();
        map1.put("data",map);

        return map1;
    }

    /**
     * 添加
     * @return
     */
    @RequestMapping("/toAddFunction")
    public String toAddRole(Model model){
        List<Function> functions = functionService.finAll();
        model.addAttribute("functions",functions);
        return "/system/add_function";
    }

    @RequestMapping("/addFunction")
    @ResponseBody
    public ResultData addRole(Function function, HttpSession session){
        User user = (User)session.getAttribute("user");
        function.setCreateBy(user.getId());
        function.setUpdateBy(user.getId());
        boolean insert = functionService.insert(function);
        if(!insert){
            resultData.buildSuccessResult("插入失败",null);
        }else{
            resultData.buildSuccessResult("插入成功",null);
        }

        return resultData;
    }
//
    /**
     * 删除
     * @param ids
     * @return
     */
    @RequestMapping("/deleteFunction")
    @ResponseBody
    public Map deleteRole(String ids){
        String[] split = ids.split(",");
        boolean flag = true;
        for(int i = 0; i < split.length;i++){
            boolean delete = functionService.delete(Integer.parseInt(split[i]));
            if(!delete){
                flag = false;
            }
        }
        Map map = new HashMap();
        if(flag){
            map.put("success",true);
            map.put("message","删除成功");
        }else{
            map.put("success",false);
            map.put("message","删除失败");
        }
        return map;
    }

    /**
     * 修改
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/toUpdateFunction")
    public String toUpdateRole(Integer id, Model model){
        List<Function> functions = functionService.finAll();
        Function function = functionService.selectFunctionById(id);
        model.addAttribute("functions",functions);
        model.addAttribute("function",function);
        return "system/edit_function";
    }
//
    @RequestMapping("/updateFunction2")
    @ResponseBody
    public Map updateRole(Function function){
        boolean update = functionService.update(function);

        Map map = new HashMap();
        if(update){
            map.put("success",true);
            map.put("message","插入成功");
        }else{
            map.put("success",false);
            map.put("message","插入失败");
        }
        return map;
    }

}
